package main

import "fmt"

func divide(dividend int, divisor int) int {
	var shang int = 0
	fuhao := 1
	if dividend < 0 {
		fuhao = -fuhao
		dividend = -dividend
	}
	if divisor < 0 {
		fuhao = -fuhao
		divisor = -divisor
	}
	s2 := divisor
	var s21 int = 1
	for dividend-s2-s2 > 0 {
		dividend = dividend - s2 - s2
		shang = shang + s21 + s21
		s2 = s2 + s2
		s21 = s21 + s21
	}
	for dividend >= divisor {
		if dividend-s2 < 0 {
			s2 = s2 >> 1
			s21 = s21 >> 1
		} else {
			dividend = dividend - s2
			shang = shang + s21
		}
	}

	if shang > 2147483646 {
		if fuhao == 1 {
			return 2147483647
		}
		if fuhao == -1 && shang > 2147483647 {
			return -2147483648
		}
	}
	fmt.Println(s2, s21, shang)
	if fuhao == 1 {
		return shang
	}
	return -shang

}

func main() {
	//fmt.Println("3:   ", divide(10, 3))
	//fmt.Println("-2:   ", divide(-214, 3))
	fmt.Println("-2:   ", divide(-2147483648, 1))
}
